#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc_core.h"


/********************************************************************************************
********************************************************************************************
    HX83200A  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    HX83200A  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    HX83200A  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    HX83200A  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    HX83200A  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
********************************************************************************************
********************************************************************************************/
void HX83200_Write_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
		//SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
    delay_ms(5);
    SSD2828_W_Array(SigMode,channel,buffer,2);

    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    delay_ms(5);
    buffer[4] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写寄存器状态  ：ok
}

void HX83200_Write51_Register(u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    if(SigMode != CMD_Mode)
        SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    delay_ms(5);
    SSD2828_W_Array(SigMode,channel,buffer,2);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    delay_ms(5);
    if(buffer[0]==RM67195)
    {
        SSD2828_W_Reg(SigMode,channel,0xBF,0xAAFB); 
    }
}

void HX83200_Read_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[2];
		u16 tmp;
    if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI
        SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);      
    else
        SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xC1,buffer[2]-1); //return package size                                
    SSD2828_W_Reg(SigMode,channel,0xC0,0x0001);                           
    buffer1[0] = 0x01;
    buffer1[1] = buffer[3];
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
    delay_ms(20);

    buffer[0] = 0x01;
    buffer[1] = buffer[3];                        
    SSD2828_W_Array(SigMode,channel,buffer,0);
    delay_ms(5);  
    SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
    SSD2828_W_Cmd(SigMode,channel,0xFF);
    for(i=0;i<buffer[2]-1;i++)
    {
  //      SSD2828_W_Cmd(SigMode,channel,0xFA);		
        tmp=SPI3_Read_u16_Data(channel);
        buffer[4+i]=tmp>>8;
        buffer[5+i]=tmp;
        delay_ms(5);
        i++;
    }
		buffer[0] = 0x2b;  
		buffer[1] = 0x02;  
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 寄存器数据  ：ok 	
}

void HX83200_Write_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[3];
		u8 i=0,j=0,k=0,l=0,temp=0;
	//	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
		delay_ms(5);
    buffer1[0] = 0x02;
    buffer1[1] = 0xB0;
		if(buffer[3]==0x81)
			buffer1[2] = 0x06;  //PAGE 6
		else
			buffer1[2] = 0x05;  //PAGE 5
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		
		for(i=0;i<29;i++)
		{
				if((buffer[3]==0x00)||(buffer[3]==0x81))	
				{
						temp|=(buffer[k+5]<<(l*2));
						if((++l==4)||(i==28))
						{
							SSD2828_W_Reg(SigMode,channel,0xBF,(u16)((temp<<8)|(0xB8-j++))); //高两位 
							temp=0;l=0;
						}
						SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[k+4]<<8)|(0xD5-i));  //低八位						
				}
				else if(buffer[3]==0x42)
				{
						temp|=(buffer[k+5]<<(l*2));
						if((++l==4)||(i==28))
						{
							SSD2828_W_Reg(SigMode,channel,0xBF,(u16)((temp<<8)|(0xDD-j++))); //高两位 
							temp=0;l=0;
						}
						SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[k+4]<<8)|(0xFA-i));  //低八位			
				}
				k+=2;
				delay_ms(1);
		}
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok  
}

void HX83200_auto_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
  
}

void HX83200_Read_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 i,j=0,k=0,MSB=0,LSB=0,temp;
		u8 buffer1[3];
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
    //SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
		delay_ms(15);
	
    buffer1[0] = 0x02;
    buffer1[1] = 0xB0;
		if(buffer[3]==0x81) //蓝色
		{
			buffer1[2] = 0x06;  //PAGE 6
		}
		else
		{
			buffer1[2] = 0x05;  //PAGE 5
		}
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
//  SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);      
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); 
		SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
		SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);                     
		for( i = 0; i<58; i++)							
		{ 
			if(i%2==0)//偶数，高八位
			{
					SSD2828_W_Cmd(SigMode,channel,0xBF);
					if(buffer[3]==0x00)
					{
							SSD2828_W_Data(SigMode,channel,0xB8-MSB);
					}
					else if(buffer[3]==0x42)
					{
							SSD2828_W_Data(SigMode,channel,0xDD-MSB);				
					}
					else if(buffer[3]==0x81)
					{
							SSD2828_W_Data(SigMode,channel,0xB8-MSB);				
					}
					delay_ms(5);
					SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA); 
					temp=SSD2828_R_Reg(SigMode,channel,0xFF);
					buffer[i+4]=(temp>>(k*2))&0x03;
					k++;		
					if(++j==4)
					{
						MSB++;
						j=0;
						k=0;
					}					
			}	
			else
			{
					SSD2828_W_Cmd(SigMode,channel,0xBF);
					if(buffer[3]==0x00)
						SSD2828_W_Data(SigMode,channel,0xD5-LSB);
					else if(buffer[3]==0x42)
						SSD2828_W_Data(SigMode,channel,0xFA-LSB);
					else if(buffer[3]==0x81)
						SSD2828_W_Data(SigMode,channel,0xD5-LSB);
					delay_ms(5);
					SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA); 
					buffer[i+4]=SSD2828_R_Reg(SigMode,channel,0xFF);
					LSB++;
			}
			delay_ms(1);
		}
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);	                              
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 Gamma数据  ：ok 
}

void HX83200_Gamma_OTP_Start(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u16 i;
    u8 buffer1[3];

    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
    delay_ms(15);
    buffer1[0] = 0x02;  //select page =E
    buffer1[1] = 0xB0;
    buffer1[2] = 0x0E;  
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
	//	delay_ms(5); 
    buffer1[0] = 0x02;  //program password
    buffer1[1] = 0xB2;
    buffer1[2] = 0xA5;  
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
//		delay_ms(5);
		for(i=0;i<3;i++)	
		{	
				buffer1[0] = 0x02;  //select group
				buffer1[1] = 0xB1;
				buffer1[2] = 0x0c+i;  
				SSD2828_W_Array(SigMode,channel,buffer1,0); 
			//	delay_ms(5); 
				buffer1[0] = 0x02;  //start to program
				buffer1[1] = 0xB3;
				buffer1[2] = 0x01;  
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				delay_ms(50);         // wait for >25ms
		}
  //  SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);

    buffer[4] = Uart_Error_None; 
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写OTP状态  ：ok  
}

void HX83200_Read_OTP_Time(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[3];
		u8 tmp=0,NX83200_OTP_Times=0;
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
    delay_ms(5);
	
		for(i=0;i<3;i++)	
		{	
				buffer1[0] = 0x02;  //select page =E
				buffer1[1] = 0xB0;
				buffer1[2] = 0x0E;  
				SSD2828_W_Array(SigMode,channel,buffer1,0); 
			
				buffer1[0] = 0x02;  //program password
				buffer1[1] = 0xB2;
				buffer1[2] = 0xA5;  
				SSD2828_W_Array(SigMode,channel,buffer1,0); 
		
				//------------------------------------
				buffer1[0] = 0x02;  //读取目标地址的高4位
				buffer1[1] = 0xB4;
				buffer1[2] = 0x04;  //05
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	

				buffer1[0] = 0x02;  //读取目标地址的低8位
				buffer1[1] = 0xB5;
				buffer1[2] = 0x35+37*i;   //13
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	
				delay_ms(5);
				//-----------------------------------
				buffer1[0] = 0x02;  //to set OTP read
				buffer1[1] = 0xB3;
				buffer1[2] = 0x04;  
				SSD2828_W_Array(SigMode,channel,buffer1,0); 	

				SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
				delay_ms(5);
				SSD2828_W_Reg(SigMode,channel,0xC1,0x0001);
				SSD2828_W_Reg(SigMode,channel,0xC0,0x0001);
				buffer1[0] = 0x01;
				buffer1[1] = 0xb6; //read reg.B6 OTP value
				SSD2828_W_Array(SigMode,channel,buffer1,0);
				delay_ms(5);
				SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);														   								
				tmp=SSD2828_R_Reg(SigMode,channel,0xFF); 
				if(tmp&0x80)
				{
					NX83200_OTP_Times++;
				}
				delay_ms(5);	
		}
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = NX83200_OTP_Times; 
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写OTP状态  ：ok	
}

void ProcessForIc2B( USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	//void *pdev;
	switch(buffer[1]&0x0F)
	{
			case 0x01:                                      //写寄存器
				HX83200_Write_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;	
			case 0x02:                                      //读寄存器
				HX83200_Read_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;	
			case 0x08:                                      //写Gamma数据    
				HX83200_Write_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
			case 0x09:                                      //读Gamma数据
				HX83200_Read_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break; 
			case 0x0A://gamma otp
				HX83200_Gamma_OTP_Start(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
			case 0x0B:
					HX83200_Read_OTP_Time(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
			default:	break;	
	}			
}


